<template>
  <div class="app-container">
    <div class="change">
      <p>
        <span>作品名</span>
        <el-input v-model="tempData.titles" :disabled="islook" class="userselect" type="text" placeholder="请输入作品名"/>
      </p>
      <p>
        <span>序号</span>
        <el-input v-model="tempData.xuhao" :disabled="islook" class="userselect" type="number" placeholder="请输入序号"/>
      </p>
      <p>
        <span>图片</span>
        <el-upload
          class="avatar-uploader"
          action
          :http-request="designUpAvatar"
          :show-file-list="false"
        >
          <img v-if="tempData.imgurl" :src="tempData.imgurl" class="avatar1" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </p>
    </div>
		<div class="submits">
		  <el-button class="save" size="small" type="primary" :loading="loading" @click="save">保存</el-button>
		  <el-button class="nosave" size="small" type="info" @click="nosave">取消</el-button>
		</div>
  </div>
</template>

<script>
import { addDaoshiCaigou,updateDaoshiCaigou } from '@/api/waimao'
import { uploadImg,addZuopin,updateZuopin,uploadFile } from '@/api/people'
export default {
  data(){
    return{
      tempData: {},
      loading: false,
      writeOradd: 0, //判断是0新增还是1修改
      islook: false
    }
  },
  mounted(){
    console.log(this.$route.params)
    if(this.$route.params.look){
      this.islook = true
    }else{
      this.islook = false
    }
    if(this.$route.params.options){
      this.writeOradd = 1
      this.tempData = JSON.parse(this.$route.params.options)
    }else{
      this.writeOradd = 0
      this.tempData = {}
    }
  },
  methods:{
    /**
     * 保存发货单
     */
    save(){
      // 如果对象为空，直接返回
      if(JSON.stringify(this.tempData) === '{}'){
        this.$message({
          message: '请填写相关信息！',
          type: 'warning'
        })
        return
      }
      this.loading = true
      //0为新增操作
      if(this.writeOradd === 0){
        addZuopin(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1)
        }).catch(() => {
          this.loading = false
        })
      }else{
        // 1是编辑修改操作
        delete this.tempData.insertTime
        delete this.tempData.updateTime
        delete this.tempData.deleteId
        updateZuopin(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1)
        }).catch(() => {
          this.loading = false
        })
      }
    },
    /**
     * 上传图片
     */
    designUpAvatar(item) {
      console.log(this.fileNum);
      var formData = new FormData();
      const loading = this.$loading({
        lock: true,
        text: "上传中请稍等...",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.8)",
      });
      formData.append("file", item.file);
      uploadFile(formData).then((res) => {
        console.log(res);
        this.$message({
          showClose: true,
          message: "添加成功",
          type: "success",
        });
        loading.close();
        this.tempData.imgurl = res;
        this.$forceUpdate()
        console.log(this.tempData.imgurl);
      });
    },
    nosave(){
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped lang="scss">
.submits {
  display: flex;
  background: #fff;
  padding: 20px 20px;
  border-radius: 8px;
  margin-top: 20px;
  // border-top: 1px solid #ccc;
  justify-content: flex-end;
  align-items: center;
  > div {
    width: 80px;
    height: 32px;
    text-align: center;
    line-height: 32px;
    color: #fff;
    font-size: 14px;
    border-radius: 4px;
    margin-left: 30px;
    cursor: pointer;
  }
  .save {
    background: #18a05e;
  }
  .nosave {
    background: #999;
  }
}
.change {
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  border-radius: 8px;
  padding: 20px 0;
  > p {
    width: 33%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      // width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
    }

    > span {
      display: block;
      width: 100px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #999 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px solid #ccc;
}

.avatar1 {
  width: 100px;
  height: 100px;
  display: block;
}
</style>